#ifndef _DICOM_WEB_SERVER__H_
#define _DICOM_WEB_SERVER__H_

#include <map>
#include <boost/function.hpp>
#include <onion/onion.h>
#include <onion/request.h>
#include <onion/response.h>
#include "dicom/http_handler.h"
#include "dicom/restful_url.h"
#include "dicom/dicom_conf.h"

namespace dicom {

typedef boost::function<HttpHandler*(onion_request*)> CreateHandlerCallback;

class Gateway;

class WebServer {
 public:
  WebServer(Gateway* gw, const DicomConf* conf);

  void Start();

  // methods:
  //    *         all methods
  //    GET,PUT   for GET & PUT methods
  void AddHandler(const std::string& url, const std::string& methods,
                  const CreateHandlerCallback& cb);

  int HandleRequest(onion_request* req, onion_response* resp);

 private:
  struct Location {
    UrlPattern pattern;
    std::string methods;
    CreateHandlerCallback handler_cb;

    Location(const std::string& pat, const std::string& _methods,
             const CreateHandlerCallback& cb)
        : pattern(pat), methods(_methods), handler_cb(cb) {}
  };

  typedef std::vector<Location*> LocationList;

  Location* MatchLocation(onion_request* req, UrlParams* params);
  void DestroyHandler(onion_request* req);

  Gateway* gw_;
  const DicomConf* conf_;
  LocationList locs_;
};
}

#endif  // _DICOM_WEB_SERVER__H_
